package v2ch01.path;

import common.Charset;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.Arrays;
import java.util.List;

/**
 * Description: path class demo
 * Created by 4790K_NSL on 2017/5/11.
 */
public class PathDemo {
    public static void main(String[] args) throws IOException {
        //D:\Develop_Environment\Config\Read_Me.txt

        Path readmePath= Paths.get("/Develop_Environment","Config","Read_Me.txt");
        System.out.println(readmePath.getFileName());
        System.out.println(Files.exists(readmePath));
        System.out.println(readmePath.toAbsolutePath());
        Path parent=readmePath.getParent();
        System.out.println(parent);

        System.out.println(parent.resolve("Read_Me.txt"));

        System.out.println(readmePath.resolveSibling("Maven"));

        System.out.println(parent.relativize(readmePath));

        Path complexPath=Paths.get("/Develop_Environment/.././Develop_Environment/Config");
        System.out.println(complexPath);
        System.out.println(complexPath.normalize());

        byte[] bytes=Files.readAllBytes(readmePath);
        String content=new String(bytes, Charset.GB2312);
        List<String> lines=Files.readAllLines(readmePath, java.nio.charset.Charset.forName(Charset.GB2312));
        System.out.println(lines);


        Path copyFile=readmePath.resolveSibling("readme_copy.txt");
        System.out.println(copyFile);

        Files.deleteIfExists(copyFile);
        Files.copy(readmePath,copyFile);


        Files.write(copyFile,("copy/n/r"+content).getBytes(Charset.GB2312));
        Files.write(copyFile,"/n/r add1".getBytes(Charset.GB2312), StandardOpenOption.APPEND);
        List<String> addLines= Arrays.asList("add line 1","add line 2");
        Files.write(copyFile,addLines, java.nio.charset.Charset.forName(Charset.GB2312));

        String content2=new String(Files.readAllBytes(copyFile),Charset.GB2312);
        System.out.println(content2);

    }
}
